package com.gbr.presentation.areaselector.impl;

import java.awt.Rectangle;

import com.gbr.application.AnimatedGifPreferences;
import com.gbr.application.action.SelectAreaAction;
import com.gbr.application.model.RecordingArea;
import com.gbr.presentation.areaselector.IAreaSelectorPresentation;
import com.ng.framework.gui.presentation.AbstractPresentationModel;
import com.ng.framework.model.IModelController;
import com.ng.framework.service.action.IActionListener;
import com.ng.framework.service.log.LogLevel;
import com.ng.framework.service.preference.IPreferenceService;
import com.ng.framework.service.preference.Preference;

public class AreaSelectorPresentationImpl extends AbstractPresentationModel implements IAreaSelectorPresentation {
    private static final String      RECORDING_AREA_ID  = "RECORDING_AREA_ID";
    private IModelController         modelController    = null;
    private IPreferenceService       preferenceService  = null;

    private AreaSelectActionListener selectAreaListener = new AreaSelectActionListener();

    @Override
    protected void activateComponent() {
        logService.log(getClass(), LogLevel.TRACE, "Activating");
        actionService.addListener(SelectAreaAction.class, selectAreaListener);
        loadPreferences();
    }

    @Override
    protected void deactivateComponent() {
        logService.log(getClass(), LogLevel.TRACE, "Deactivating");
        actionService.removeListener(SelectAreaAction.class, selectAreaListener);
    }

    public final void bindService(IModelController service) {
        modelController = service;
    }

    public final void unbindService(IModelController service) {
        modelController = null;
    }

    public final void bindService(IPreferenceService service) {
        preferenceService = service;
    }

    public final void unbindService(IPreferenceService service) {
        preferenceService = null;
    }

    @Override
    public void setRecordingArea(int x, int y, int width, int height) {
        logService.log(getClass(), LogLevel.DEBUG, "Setting recording area to: Location(" + x + "," + y + "), Size(" + width + "," + height + ")");
        firePropertyChange(IAreaSelectorPresentation.ACTIVATE, false, true);

        RecordingArea recordingArea = modelController.getModel(RecordingArea.class, RECORDING_AREA_ID);
        if (recordingArea == null) {
            recordingArea = new RecordingArea(RECORDING_AREA_ID);
            recordingArea.setRect(new Rectangle(x,y,width,height));
        }

        modelController.merge(recordingArea);
    }

    private void loadPreferences() {
        try {
            // make sure preferences are available
            if (!AnimatedGifPreferences.exists(preferenceService)) {
                return;
            }

            Preference pref = preferenceService.getSystemPreference(AnimatedGifPreferences.Name);
            if (pref != null) {
                RecordingArea ra = new RecordingArea(RECORDING_AREA_ID);
                ra.setRect(new Rectangle((Integer) pref.get(AnimatedGifPreferences.RecordingArea_X),
                                         (Integer) pref.get(AnimatedGifPreferences.RecordingArea_Y),
                                         (Integer) pref.get(AnimatedGifPreferences.RecordingArea_WIDTH), 
                                         (Integer) pref.get(AnimatedGifPreferences.RecordingArea_HEIGHT)));
                
                modelController.merge(ra);
            }
        } catch (Exception e) {
            //e.printStackTrace();
            logService.log(getClass(), LogLevel.WARN, "No preference found for Recording Area.");
        }
    }

    @Override
    public void abort() {
        firePropertyChange(IAreaSelectorPresentation.ACTIVATE, false, true);
    }

    class AreaSelectActionListener implements IActionListener<SelectAreaAction> {
        @Override
        public void actionPerformed(final SelectAreaAction action) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    firePropertyChange(IAreaSelectorPresentation.ACTIVATE, action.isSelect(), null);
                }
            }).start();
        }
    }
}
